package com.amazon.ags.client;

import android.content.BroadcastReceiver;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.ServiceConnection;
import android.content.pm.PackageManager;
import android.content.pm.ResolveInfo;
import android.os.Bundle;
import android.os.Handler;
import android.os.IBinder;
import android.os.Message;
import android.os.Messenger;
import android.os.Parcel;
import android.os.RemoteException;
import android.text.TextUtils;
import android.util.Log;
import android.widget.RemoteViews;
import com.amazon.ags.api.AmazonGamesFeature;
import com.amazon.ags.api.AmazonGamesStatus;
import com.amazon.ags.client.authentication.AuthenticationServiceProxy;
import com.amazon.ags.client.util.YesNoMaybe;
import com.amazon.ags.overlay.PopUpManager;
import java.util.ArrayList;
import java.util.EnumSet;
import java.util.Iterator;

public class ServiceProxy
  implements AmazonGamesService
{
  private static final String FEATURE_NAME = "AGC";
  private static final String SERVICE_CLASS_NAME = "com.amazon.ags.app.service.AmazonGamesService";
  private static final String SERVICE_PACKAGE_NAME = "com.amazon.ags.app";
  private static final String TAG = "AGC_" + ServiceProxy.class.getSimpleName();
  private final AuthenticationServiceProxy authenticationProxy;
  private YesNoMaybe authorizedState = YesNoMaybe.MAYBE;
  private final EnumSet<AmazonGamesFeature> features;
  private YesNoMaybe optInState = YesNoMaybe.MAYBE;
  private Messenger serviceMessenger = null;
  private String sessionId;
  private AmazonGamesStatus status = AmazonGamesStatus.INITIALIZING;
  private final Handler statusHandler;
  private IBinder synchronousBinder = null;

  public ServiceProxy(Handler paramHandler, EnumSet<AmazonGamesFeature> paramEnumSet)
  {
    this.statusHandler = paramHandler;
    this.authenticationProxy = new AuthenticationServiceProxy(this);
    if (paramEnumSet == null)
    {
      this.features = EnumSet.noneOf(AmazonGamesFeature.class);
      return;
    }
    this.features = paramEnumSet;
  }

  private void authorize()
  {
    Message localMessage = Message.obtain();
    Bundle localBundle = new Bundle();
    ArrayList localArrayList = buildFeatureList();
    localMessage.what = 25;
    localBundle.putStringArrayList("FEATURE_LIST", localArrayList);
    localMessage.setData(localBundle);
    localMessage.replyTo = new Messenger(new Handler()
    {
      public void handleMessage(Message paramAnonymousMessage)
      {
        Log.d(ServiceProxy.TAG, "Handling authorize callback");
        Bundle localBundle = paramAnonymousMessage.getData();
        String str = localBundle.getString("AUTH_RESULT");
        Log.d(ServiceProxy.TAG, "authResult: " + str);
        if ("AUTHORIZED".equals(str))
        {
          ServiceProxy.access$502(ServiceProxy.this, YesNoMaybe.YES);
          ServiceProxy.this.onStateChange();
          RemoteViews localRemoteViews = (RemoteViews)localBundle.getParcelable("WELCOME_BACK_TOAST");
          if (localRemoteViews != null)
          {
            Log.d(ServiceProxy.TAG, "Generating a welcome back toast");
            PopUpManager.getInstance().executeWelcomeBackPopUp(localRemoteViews, 3);
          }
        }
        do
        {
          return;
          if ("INVALID_SESSION".equals(str))
          {
            ServiceProxy.this.changeStatus(AmazonGamesStatus.INVALID_SESSION);
            return;
          }
          if ("CANNOT_AUTHORIZE".equals(str))
          {
            ServiceProxy.this.changeStatus(AmazonGamesStatus.CANNOT_AUTHORIZE);
            return;
          }
        }
        while (!"NOT_AUTHORIZED".equals(str));
        ServiceProxy.this.changeStatus(AmazonGamesStatus.NOT_AUTHORIZED);
      }
    });
    try
    {
      sendMessage(localMessage);
      return;
    }
    catch (RemoteException localRemoteException)
    {
      Log.e(TAG, "Unable to send Message to Service: ", localRemoteException);
      changeStatus(AmazonGamesStatus.CANNOT_BIND);
    }
  }

  private void bindToAsynchronousService(Context paramContext)
  {
    ServiceConnection local1 = new ServiceConnection()
    {
      public void onServiceConnected(ComponentName paramAnonymousComponentName, IBinder paramAnonymousIBinder)
      {
        ServiceProxy.access$002(ServiceProxy.this, new Messenger(paramAnonymousIBinder));
        ServiceProxy.this.onBindChange();
      }

      public void onServiceDisconnected(ComponentName paramAnonymousComponentName)
      {
        ServiceProxy.access$002(ServiceProxy.this, null);
        ServiceProxy.this.onBindChange();
      }
    };
    Intent localIntent = createBindIntent();
    localIntent.setAction("BindAsynchronous");
    try
    {
      boolean bool = paramContext.bindService(localIntent, local1, 1);
      Log.d(TAG, "binding result:" + bool);
      return;
    }
    catch (SecurityException localSecurityException)
    {
      Log.e(TAG, "unable to bind to asynchronous service: ", localSecurityException);
      changeStatus(AmazonGamesStatus.CANNOT_BIND);
    }
  }

  private void bindToSynchronousService(Context paramContext)
  {
    ServiceConnection local2 = new ServiceConnection()
    {
      public void onServiceConnected(ComponentName paramAnonymousComponentName, IBinder paramAnonymousIBinder)
      {
        ServiceProxy.access$202(ServiceProxy.this, paramAnonymousIBinder);
        ServiceProxy.this.onBindChange();
      }

      public void onServiceDisconnected(ComponentName paramAnonymousComponentName)
      {
        ServiceProxy.access$202(ServiceProxy.this, null);
        ServiceProxy.this.onBindChange();
      }
    };
    Intent localIntent = createBindIntent();
    localIntent.setAction("BindSynchronous");
    try
    {
      paramContext.bindService(localIntent, local2, 1);
      return;
    }
    catch (SecurityException localSecurityException)
    {
      Log.e(TAG, "unable to bind to synchronous service: ", localSecurityException);
      changeStatus(AmazonGamesStatus.CANNOT_BIND);
    }
  }

  private ArrayList<String> buildFeatureList()
  {
    ArrayList localArrayList = new ArrayList(this.features.size());
    Iterator localIterator = this.features.iterator();
    while (localIterator.hasNext())
      localArrayList.add(((AmazonGamesFeature)localIterator.next()).toString());
    return localArrayList;
  }

  private void changeStatus(AmazonGamesStatus paramAmazonGamesStatus)
  {
    if (paramAmazonGamesStatus != this.status)
    {
      Log.d(TAG, "Changing Status from:" + this.status + " to: " + paramAmazonGamesStatus);
      this.status = paramAmazonGamesStatus;
      notifyCaller();
    }
  }

  private Intent createBindIntent()
  {
    Intent localIntent = new Intent();
    localIntent.setClassName("com.amazon.ags.app", "com.amazon.ags.app.service.AmazonGamesService");
    localIntent.putExtra("CLIENT_VERSION", "1.0.0");
    return localIntent;
  }

  private boolean initSession()
  {
    try
    {
      this.sessionId = transact(24, Parcel.obtain()).readString();
      Log.d(TAG, "Session Id:" + this.sessionId);
      if (TextUtils.isEmpty(this.sessionId))
      {
        Log.e(TAG, "Could not obtain session");
        return false;
      }
      return true;
    }
    catch (RemoteException localRemoteException)
    {
      Log.e(TAG, "Could not obtain session");
    }
    return false;
  }

  private boolean isAuthenticated()
  {
    return (isBound()) && (this.authenticationProxy.isAuthenticated());
  }

  private boolean isBound()
  {
    if ((this.synchronousBinder == null) || (this.serviceMessenger == null))
    {
      Log.d(TAG, "synchronousBinder:" + this.synchronousBinder + " serviceMessengerClient:" + this.serviceMessenger);
      return false;
    }
    Log.d(TAG, "Client is bound to service");
    return true;
  }

  private boolean isServiceAvailable(Context paramContext)
  {
    Intent localIntent = createBindIntent();
    ResolveInfo localResolveInfo = paramContext.getPackageManager().resolveService(localIntent, 0);
    boolean bool = false;
    if (localResolveInfo != null)
      bool = true;
    return bool;
  }

  private void notifyCaller()
  {
    Message localMessage = this.statusHandler.obtainMessage();
    localMessage.obj = this.status;
    this.statusHandler.sendMessage(localMessage);
  }

  private void onBindChange()
  {
    if ((isBound()) && (this.status == AmazonGamesStatus.INITIALIZING))
      if (initSession())
        authorize();
    while ((isBound()) || (this.status != AmazonGamesStatus.SERVICE_CONNECTED))
    {
      return;
      changeStatus(AmazonGamesStatus.CANNOT_BIND);
      return;
    }
    changeStatus(AmazonGamesStatus.SERVICE_DISCONNECTED);
  }

  private void onStateChange()
  {
    boolean bool = isAuthenticated();
    if ((this.authorizedState == YesNoMaybe.YES) && (this.optInState == YesNoMaybe.YES) && (isBound()) && (bool))
      changeStatus(AmazonGamesStatus.SERVICE_CONNECTED);
    while ((this.authorizedState != YesNoMaybe.YES) || (this.optInState != YesNoMaybe.NO))
      return;
    changeStatus(AmazonGamesStatus.SERVICE_NOT_OPTED_IN);
  }

  private void registerBroadcastReceiver(Context paramContext)
  {
    IntentFilter localIntentFilter = new IntentFilter("OPT_IN_STATUS_BROADCAST");
    paramContext.registerReceiver(new AmazonGamesBroadcastReceiver(null), localIntentFilter);
  }

  public final void bind(Context paramContext)
  {
    if (isServiceAvailable(paramContext))
    {
      bindToAsynchronousService(paramContext);
      bindToSynchronousService(paramContext);
      registerBroadcastReceiver(paramContext);
      return;
    }
    Log.e(TAG, "Amazon Games Service is not available");
    changeStatus(AmazonGamesStatus.CANNOT_BIND);
  }

  public final AmazonGamesStatus getStatus()
  {
    return this.status;
  }

  public final boolean isReady()
  {
    return this.status == AmazonGamesStatus.SERVICE_CONNECTED;
  }

  public final void sendMessage(Message paramMessage)
    throws RemoteException
  {
    if (paramMessage.getData() == null)
      paramMessage.setData(new Bundle());
    paramMessage.getData().putString("CLIENT_VERSION", "1.0.0");
    paramMessage.getData().putString("SESSION_ID", this.sessionId);
    if (this.serviceMessenger == null)
    {
      Log.e(TAG, "Service is not bound");
      return;
    }
    this.serviceMessenger.send(paramMessage);
  }

  public final Parcel transact(int paramInt, Parcel paramParcel)
    throws RemoteException
  {
    Parcel localParcel = Parcel.obtain();
    if (this.synchronousBinder == null)
    {
      Log.e(TAG, "transact() was called while disconnected");
      return localParcel;
    }
    this.synchronousBinder.transact(paramInt, paramParcel, localParcel, 0);
    return localParcel;
  }

  private class AmazonGamesBroadcastReceiver extends BroadcastReceiver
  {
    private AmazonGamesBroadcastReceiver()
    {
    }

    public void onReceive(Context paramContext, Intent paramIntent)
    {
      String str = paramIntent.getExtras().getString("OPT_IN_STATUS");
      Log.d(ServiceProxy.TAG, "Game received broadcast from service with opt-in status [" + str + "]");
      if ("NOT_OPTED_IN".equals(str))
      {
        ServiceProxy.access$802(ServiceProxy.this, YesNoMaybe.NO);
        ServiceProxy.this.onStateChange();
      }
      while (!"OPTED_IN".equals(str))
        return;
      ServiceProxy.access$802(ServiceProxy.this, YesNoMaybe.YES);
      ServiceProxy.this.onStateChange();
    }
  }
}

/* Location:           E:\android\Toy Defense 2_dex2jar.jar
 * Qualified Name:     com.amazon.ags.client.ServiceProxy
 * JD-Core Version:    0.6.2
 */